home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ für Kids
/
C++ for kids.iso
/
SETUP
/
US
/
CBUILDER
/
DATA.Z
/
STRING.CC
< prev
next >
Wrap
C/C++ Source or Header
|
1997-02-13
|
23KB
|
742 lines
/***************************************************************************
*
* string.cc - Definitions for the Standard Library string classes
*
* $Id: string.cc,v 1.76 1995/09/29 22:28:50 smithey Exp $
*
***************************************************************************
*
* (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
* ALL RIGHTS RESERVED
*
* The software and information contained herein are proprietary to, and
* comprise valuable trade secrets of, Rogue Wave Software, Inc., which
* intends to preserve as trade secrets such software and information.
* This software is furnished pursuant to a written license agreement and
* may be used, copied, transmitted, and stored only in accordance with
* the terms of such license and with the inclusion of the above copyright
* notice. This software and information or any other copies thereof may
* not be provided or otherwise made available to any other person.
*
* Notwithstanding any other lease or license that may pertain to, or
* accompany the delivery of, this computer software and information, the
* rights of the Government regarding its use, reproduction and disclosure
* are as set forth in Section 52.227-19 of the FARS Computer
* Software-Restricted Rights clause.
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7013.
* Contractor/Manufacturer is Rogue Wave Software, Inc.,
* P.O. Box 2328, Corvallis, Oregon 97339.
*
* This computer software and information is distributed with "restricted
* rights." Use, duplication or disclosure is subject to restrictions as
* set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
* Computer Software-Restricted Rights (April 1985)." If the Clause at
* 18-52.227-74 "Rights in Data General" is specified in the contract,
* then the "Alternate III" clause applies.
*
**************************************************************************/
#ifndef RWSTD_NO_NAMESPACE
namespace std {
#endif
//
// Members for class string_ref
//
#ifndef RWSTD_NO_STATIC_DEF3
template <class charT, class traits, class Allocator >
const null_string_ref_rep<charT, traits, Allocator>
basic_string<charT, traits, Allocator>::nullref
= null_string_ref_rep<charT, traits, Allocator>();
#endif
//
// Members for class basic_string
//
template <class charT, class traits, class Allocator >
const basic_string<charT, traits, Allocator>::size_type
basic_string<charT, traits, Allocator>::npos
= (basic_string<charT, traits, Allocator>::size_type) -1;
template <class charT, class traits, class Allocator >
string_ref<charT, traits, Allocator> *
basic_string<charT, traits, Allocator>::getRep (size_type capac,
size_type nchar,
Allocator& alloc)
{
if ((capac | nchar) == 0)
{
getNullRep()->addReference();
return getNullRep();
}
string_ref<charT, traits, Allocator> * ret =
(string_ref<charT, traits, Allocator> *) alloc.allocate(
(capac+1)*sizeof(charT) + sizeof(string_ref_rep<Allocator>));
//
// Initialize the string_ref, then
// initialize each character in the buffer. Allocator had better
// provide a placement new for this purpose, if appropriate.
//
new (ret) string_ref<charT,traits,Allocator>;
charT * d = ret->data();
for (size_type i = 0; i <= capac; i++, d++)
new (d) charT;
ret->capacity_ = capac;
ret->setRefCount(1);
ret->data()[ret->nchars_ = nchar] = traits::eos(); // Terminating null
return ret;
}
template <class charT, class traits, class Allocator >
basic_string<charT, traits, Allocator>::basic_string (
const basic_string<charT, traits, Allocator> & s,
size_type pos,
size_type n,
const Allocator& alloc)
{
RWSTD_THROW(pos > s.length(), out_of_range, rwse_PosBeyondEndOfString);
size_type rlen = min(n, s.length() - pos);
size_type maxlen = max(n == npos ? 0 : n, rlen);
alloc_ = alloc;
data_ = getRep(maxlen, rlen, alloc_)->data();
traits::copy(data_, &s.data_[pos], rlen);
}
template <class charT, class traits, class Allocator >
basic_string<charT, traits, Allocator>::basic_string (
const charT* s,
size_type n,
const Allocator& alloc)
{
RWSTD_THROW(s == 0, logic_error, rwse_UnexpectedNullPtr);
alloc_ = alloc;
data_ = getRep(n, n, alloc_)->data();
traits::copy(data_, s, n);
}
template <class charT, class traits, class Allocator >
basic_string<charT, traits, Allocator>::basic_string (
const charT* s,
const Allocator& alloc)
{
RWSTD_THROW(s == 0, logic_error, rwse_UnexpectedNullPtr);
size_type len = traits::length(s);
alloc_ = alloc;
data_ = getRep(len, len, alloc_)->data();
traits::copy(data_, s, len);
}
template <class charT, class traits, class Allocator >
basic_string<charT, traits, Allocator>::basic_string (
size_type n,
charT c,
const Allocator& alloc)
{
RWSTD_THROW(n == npos, length_error, rwse_InvalidSizeParam);
alloc_ = alloc;
data_ = getRep(n, n, alloc_)->data();
while (n--) traits::assign(data_[n], c);
}
template <class charT, class traits, class Allocator >
basic_string<charT, traits, Allocator> &
basic_string<charT, traits, Allocator>::operator= (
const basic_string<charT, traits, Allocator>& str)
{
str.pref()->addReference();
pref()->unLink(alloc_);
data_ = str.data_;
return *this;
}
template <class charT, class traits, class Allocator >
basic_string<charT, traits, Allocator> &
basic_string<charT, traits, Allocator>::operator= (const charT* s)
{
RWSTD_THROW(s == 0, logic_error, rwse_UnexpectedNullPtr);
if (traits::eq(*s, eos()))
{
if (pref()->references() == 1)
{
pref()->nchars_ = 0;
traits::assign(data_[0], eos());
}
else
{
pref()->unLink(alloc_);
getNullRep()->addReference();
data_ = getNullRep()->data();
}
return *this;
}
return replace(0, length(), s, traits::length(s));
}
template <class charT, class traits, class Allocator >
basic_string<charT, traits, Allocator> &
basic_string<charT, traits, Allocator>::append (
const basic_string<charT, traits, Allocator>& str,
size_type pos,
size_type n)
{
RWSTD_THROW(pos > str.length(), out_of_range, rwse_PosBeyondEndOfString);
size_type rlen = min(n, str.length() - pos);
RWSTD_THROW(length() >= npos - rlen,
length_error,
rwse_ResultLenInvalid);
replace(length(), 0, str.data(), str.length(), pos, n);
return *this;
}
template <class charT, class traits, class Allocator >
basic_string<charT, traits, Allocator> &
basic_string<charT, traits, Allocator>::assign (
const basic_string<charT, traits, Allocator>& str,
size_type pos,
size_type n)
{
RWSTD_THROW(pos > str.length(), out_of_range, rwse_PosBeyondEndOfString);
size_type rlen = min(n, str.length() - pos);
return replace(0, length(), str, pos, rlen);
}
template <class charT, class traits, class Allocator >
basic_string<charT, traits, Allocator> &
basic_string<charT, traits, Allocator>::insert (
size_type pos1,
const basic_string<charT, traits, Allocator>& str,
size_type pos2,
size_type n)
{
RWSTD_THROW(pos1 > length() || pos2 > str.length(),
out_of_range,